package com.taotao.order.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.mapper.TbOrderItemMapper;
import com.taotao.mapper.TbOrderMapper;
import com.taotao.mapper.TbOrderShippingMapper;
import com.taotao.order.dao.JedisClient;
import com.taotao.order.pojo.Order;
import com.taotao.order.service.OrderService;
import com.taotao.pojo.TbOrder;
import com.taotao.pojo.TbOrderItem;
import com.taotao.pojo.TbOrderItemExample;
import com.taotao.pojo.TbOrderShipping;
import com.taotao.pojo.TbOrderShippingExample;

@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private TbOrderMapper orderMapper;
	@Autowired
	private TbOrderItemMapper orderItemMapper;
	@Autowired
	private TbOrderShippingMapper orderShippingMapper;
	@Autowired
	private JedisClient jedisClient;
	
	//订单id
	@Value("${ORDER_ID_KEY}")
	private String ORDER_ID_KEY;
	
	@Value("${ORDER_ID_INIT}")
	private Long ORDER_ID_INIT;
	//订单项
	@Value("${ORDER_ITEM_KEY}")
	
	private String ORDER_ITEM_KEY;
	@Override
	public TaotaoResult createOrder(Order order) throws Exception {

		Long incr = jedisClient.incr(ORDER_ID_KEY);
		String orderId=incr+ORDER_ID_INIT+"";
		//生成订单
		order.setOrderId(orderId);
		order.setStatus(1);
		Date date = new Date();
		order.setCreateTime(date);
		order.setUpdateTime(date);
		order.setBuyerRate(0);
		
	
		//生成订单项
		List<TbOrderItem> orderItems = order.getOrderItems();

		for(TbOrderItem orderItem : orderItems) {
			Long id = jedisClient.incr(ORDER_ITEM_KEY);
			orderItem.setId(id+"");
			orderItem.setOrderId(orderId);
			Long price = orderItem.getPrice();
			Integer num = orderItem.getNum();
			orderItem.setTotalFee(price*num);
			
			orderItemMapper.insert(orderItem);
		}
		
		
		TbOrderShipping orderShipping = order.getOrderShipping();
		orderShipping.setOrderId(orderId);
		orderShipping.setCreated(date);
		orderShipping.setUpdated(date);
		//创建物流信息
		orderShippingMapper.insert(orderShipping);
		//创建订单
		orderMapper.insert(order);
		return TaotaoResult.ok(orderId);
	}
	@Override
	public TaotaoResult getOrderById(String orderId) throws Exception {
		TbOrder tbOrder = orderMapper.selectByPrimaryKey(orderId);
		if(tbOrder==null) {
			return TaotaoResult.build(400, "该订单不存在");
		}
		Order order = new Order();
		BeanUtils.copyProperties(tbOrder, order);
		
		TbOrderItemExample orderItemExample=new TbOrderItemExample();
		orderItemExample.createCriteria().andOrderIdEqualTo(orderId);
		List<TbOrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
		order.setOrderItems(orderItems);
		
		TbOrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
		order.setOrderShipping(orderShipping);
		return TaotaoResult.ok(order);
	}

}
